/**
 *
 * 获取uuid
 */
export function getUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
    /[xy]/g,
    c => (c === 'x' ? (Math.random() * 16 | 0) : 8).toString(16)
  )
}
const env = import.meta.env
// 清除第一级的假值键或空数组(属性)
export function ClearnEmpryProp(obj: any) {
  if (!obj) return null;
  if (typeof obj === "object") {
    const tempInfo: Record<string | number, any> = {};
    Object.keys(obj).forEach((key) => {
      const value = obj[key];
      if (Array.isArray(value)) {
        if (value.length) {
          tempInfo[key] = value;
        }
      } else if (typeof obj[key] === "boolean" || obj[key] === 0) {
        tempInfo[key] = obj[key];
      } else if (obj[key]) {
        tempInfo[key] = obj[key];
      }
    });
    return tempInfo;
  }
  return obj;
}

const flotOrIntegerRege = /(\d*)\.?(\d*)$/;
const ApiBase = env.VITE_SERVER_API_BASE;
export function getImgUrl(id: string | number, token = sessionStorage.getItem(env.VITE_TOKEN_KEY)) {
  return `${ApiBase}/sysFileInfo/download?id=${id}&token=${token}`;
}
/**
 *
 * @param  number  将数字改为千分位
 * @param  flotlength 小数位数长度
 * @param  seq 千分位符号
 * @param  dec_point 小数点符号
 *
 * @returns
 */
export function NumberToThousandth(
  number: string | number = 0,
  flotlength = 2,
  seq = ",",
  dec_point = "."
) {
  if (!number) return `0${dec_point}${"".padStart(flotlength, "0")}`;
  const numberString = number.toString();
  return numberString.replace(flotOrIntegerRege, (_, int, flot) => {
    // 整数位
    let tempStr = "";
    for (let strLen = int.length; strLen >= 0; strLen -= 3) {
      if (strLen > 3) {
        tempStr = `${seq}${int.substring(strLen - 3, strLen)}` + tempStr;
      } else {
        tempStr = `${int.substring(0, strLen)}` + tempStr;
      }
    }
    // 小数位
    let flotNumber = "";
    if (flot) {
      flotNumber = flot.slice(0, flotlength).padEnd(flotlength, "0");
    } else {
      flotNumber = "".padEnd(flotlength, "0");
    }
    return `${tempStr}${flotlength ? dec_point : ""}${flotNumber}`;
  });
}



// url编码工具
export const encURI = (string: string) => window.encodeURIComponent(string);

/**
 *
 * @param {Array} list  要提取值的列表
 * @param {String} key 要提取的键名
 * @param {String} filterKey 提取键名前,需要某个键名具备值(条件过滤)
 * @param {Function} cb 如果传入回调函数,那么在提取值时,会采用cb返回的值
 */
export function getValueByKey<T = any>(list: T[], key: keyof T, filterKey: keyof T, cb?: (p: T) => string | number) {
  if (Array.isArray(list) && key) {
    if (filterKey) {
      list = list.filter((item) => item[filterKey]);
    }
    return list.map((item) => (cb ? cb(item) : item[key]));
  }
  return [];
}

/**
 *
 * @param {Array} list  要提取集合值的列表
 * @param {String} key  要取值的键
 * @param {String} filterKey  检测某个键是否有值,如果没有,则这条记录会被过滤统计
 * @returns
 */
export function getSetArrayByKey<T = any>(list: T[], key: keyof T, filterKey: keyof T) {
  return Array.from(new Set(getValueByKey(list, key, filterKey)));
}
